// check integer
owl_byte owl_check_wchar_shift_jis(owl_wchar ch)
{
  if (ch >= OWL_MIN_SHIFT_JIS
      && ch <= OWL_MAX_SHIFT_JIS
      && owl_shift_jis_bool_table[ch] == 1)
    return ch > 0xFF ? 2 : 1;
  return 0;
}

// check encoded
owl_byte owl_check_wchar_enc_shift_jis(owl_byte * src, owl_umax size)
{
  // check params
  if (owl_check_ptr(src, size) == owl_false)
    goto err;
  // check if it is a 1 or 2 byte character
  if (owl_check_wchar_shift_jis((owl_wchar) *src) != 0)
    return 1;
  else if (size >= 2 && 
           owl_check_wchar_shift_jis((owl_wchar) ((*src << 8) + *(src + 1))) != 0)
    return 2;
  // bad encoded character
  err:
  return 0;
}

// get integer
owl_wchar owl_get_wchar_shift_jis(owl_byte * src, owl_umax size)
{
  // check params
  owl_byte check = owl_check_wchar_enc_shift_jis(src, size);
  if (check == 0)
    return OWL_INV_SHIFT_JIS;
  else if (check == 1)
    return (owl_wchar) *src;
  else // check == 2
    return (owl_wchar) ((*src << 8) + *(src + 1));
}

// write encoded
owl_byte * owl_write_wchar_enc_shift_jis(owl_wchar ch, owl_byte * dest, owl_umax size)
{
  // check params
  owl_byte ch_size = owl_check_wchar_shift_jis(ch);
  if (ch_size == 0 || owl_check_ptr(dest, size) == owl_false || ch_size > size)
    return NULL;
  if (ch_size == 1)
    *dest = (owl_byte) ch;
  else /* ch_size == 2 */ {
    dest[0] = (owl_byte) (ch >> 8);
    dest[1] = (owl_byte) (ch & 0xFF);
  }
  return dest + ch_size;
}

// print integer
owl_byte owl_print_wchar_shift_jis(owl_wchar ch)
{
  // check params
  owl_byte size = owl_check_wchar_shift_jis(ch);
  if (size == 0)
    printf(OWL_INV_SHIFT_JIS_STR);
  else if (size == 1)
    printf("%02llX", ch);
  else // size == 2
    printf("%04llX", ch);
  return size;
}

// print encoded
owl_byte owl_print_wchar_enc_shift_jis(owl_byte * src, owl_umax size)
{
  // check params
  owl_wchar ch = owl_get_wchar_shift_jis(src, size);
  if (ch == OWL_INV_SHIFT_JIS) {
    printf(OWL_INV_ENC_SHIFT_JIS_STR);
    return 0;
  } else if (ch < 0x100) {
    printf("%02llX", ch);
    return 1;
  } else /* size == 2 */ {
    printf("%04llX", ch);
    return 2;
  }
}
